<?php
/**
 * Created by PhpStorm.
 * User: boge
 * Date: 2017/2/7
 * Time: 11:20
 */

require("WebSocket.php");

class Test {

    private $socket;

    function __construct()
    {

        $this->socket = new \net\socket\WebSocket();

        $con = $this->socket->connect();
        if ($con)
        {
            $this->readData();
        }
    }

    private function readData() {
        $input = $this->socket->recv();
        if ($input) {
            $time = $input->getLong();
            $action = $input->getInt();
            var_dump($time);
            var_dump($action);
            $len = $input->getInt();
            var_dump("len=".$len."\n");
            if ($action > 1) {

                var_dump($input->getUTF());
                var_dump($input->getInt());
                var_dump($input->getUTF());
                var_dump($input->getLong());
//                var_dump($input->getUTF());
//                var_dump($input->getUInt());
                var_dump($input->getUTF());
                var_dump($input->getFloat());
                var_dump($input->getUTF());
            } else {
                $this->send();
                $this->throb();
            }
            $this->readData();
        } else {
            $this->socket->disconnect();
            echo "断开连接";
        }
    }

    /**
     * 心跳包
     */
    public function throb() {
        $this->socket->sendData(1);
    }

    private function send() {
        $bytes = new Binary();


        $bytes->addUTF("测试int");
        $bytes->addInt(12345);
        $bytes->addUTF("测试long");
        $bytes->addLong(987654321);
//		$bytes->addUTF("测试uint");
//        $bytes->addUInt(55555);
        $bytes->addUTF("测试float");
        $bytes->addFloat(654321);
        $bytes->addUTF("测试发送结束!");

        $this->socket->sendData(10001, $bytes);
    }

}

new Test();

?>